using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Main.users
{
    public partial class Edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MembershipUser currentUser = Membership.GetUser();
            
            if (currentUser != null)
            {
                // Get extra info from currentUser
                DataTable UserTable = DataAccess.GetUserByUserName(currentUser.UserName);
                // DataTable UserTable = DataAccess.GetUserByUserName("bsapp100");
                DataRowCollection Rows = UserTable.Rows;
                DataRow UserRow = UserTable.Rows[0];
                
                // Grab new info from POST
                if ((Request.RequestType == "POST") &&
                    (EmailRegularExpressionValidator.IsValid == true) &&
                    (EmailRequiredFieldValidator.IsValid == true) &&
                    (USPhoneRegularExpressionValidator.IsValid == true))
                {
                    // Update User info

                    // Did Private Change?
                    if ((bool)Request["PrivateCheckBox"] != (bool)UserRow["IsAnonymous"])
                    {
                        UserRow.BeginEdit();
                        UserRow["IsAnonymous"] = (bool)Request["PrivateCheckBox"];
                        UserRow.EndEdit();
                        UserRow.AcceptChanges();
                    }

                    // Did the e-mail address change?
                    if ((Request["EmailTextBox"] != "") &&
                       (currentUser.Email != Request["EmailTextBox"]))
                    {
                        currentUser.Email = Request["EmailTextBox"];
                    }

                    // Did the mobile e-mail domain name change?
                    if ((int)UserRow["SMSEmailID"] != (int)Request["MobileProvidorDropDownList"])
                    {
                        UserRow.BeginEdit();
                        UserRow["SMSEmailID"] = (int)Request["MobileProvidorDropDownList"];
                        UserRow.EndEdit();
                        UserRow.AcceptChanges();
                    }

                    // Did the phone number change?
                    if (UserRow["MobileAlias"] != Request["MobileTextBox"])
                    {
                        UserRow.BeginEdit();
                        UserRow["MobileAlias"] = Request["MobileTextBox"];
                        UserRow.EndEdit();
                        UserRow.AcceptChanges();
                    }
                }
                // Populate Form Elements with User Data
                // Populate Mobile Providor Dropdown with data
                DataTable SMSEmailsTable = DataAccess.GetAllSMSEmails();
                DataRowCollection SMSEmailsRows = SMSEmailsTable.Rows;
                foreach (DataRow SMSRow in SMSEmailsRows)
                {
                    string MobileListItemText = (string)SMSRow["Name"] + " [@" + (string)SMSRow["TopLevelDomain"] + "]";
                    string MobileListItemValue = ((int)SMSRow["ID"]).ToString();

                    MobileProvidorDropDownList.Items.Add(
                        new ListItem(MobileListItemText, MobileListItemValue, true)
                    );
                }

                
                PrivateCheckBox.Checked = (bool)UserRow["IsAnonymous"];
                UsernameLabel.Text = "Welcome, " + currentUser.UserName + "!";
                if(UserRow["MobileAlias"] != System.DBNull.Value)
                {
                    MobileTextBox.Text = (string)UserRow["MobileAlias"];
                }

                if (UserRow["SMSEmailID"] != System.DBNull.Value)
                {
                    MobileProvidorDropDownList.SelectedIndex = (int)UserRow["SMSEmailID"];
                }
                EmailTextBox.Text = currentUser.Email;
            }
            else
            {
                Response.Redirect(ResolveUrl("Login.aspx"));
            }

        }

        protected void Submit_ServerClick(object sender, EventArgs e)
        {
        }
    }
}
